package com.gxa.xly2024.service.impl;

import com.gxa.xly2024.mapper.UserMapper;
import com.gxa.xly2024.pojo.User;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl {
    @Resource
    private UserMapper userMapper;

    public Map<String, Object> selectUserById(Integer id) {
        User user = this.userMapper.selectUserById(id);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "查询id成功");
        map.put("data", user);
        return map;
    }

    public Map<String, Object> selectUserByLimit(Integer page, Integer limit) {
        int index = (page - 1) * limit;
        List<User> users = this.userMapper.selectUserByLimit(index, limit);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "分页查询成功");
        map.put("count", this.userMapper.selectCount());   // 查询当前表总条数
        map.put("data", users);
        return map;
    }

    public Map<String, Object> insert(User user) {
        this.userMapper.insert(user);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "新增用户成功");
        map.put("data", user);
        return map;
    }

    public Map<String, Object> updateById(User user) {
        this.userMapper.updateById(user);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "更新用户成功");
        map.put("data", user);
        return map;
    }

    public Map<String, Object> deleteById(Integer id) {
        this.userMapper.deleteById(id);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "删除用户成功");
        map.put("id", id);
        return map;
    }
}
